(function() {
  //ui2model
  var parse2model = function(ui) {
    return {
      posterurl: ui.children[0].src,
      posterwidth: ui.children[0].width,
      posterheight: ui.children[0].height,
      headimg: ui.children[2].src,
      headimgx: ui.children[2].x,
      headimgy: ui.children[2].y,
      headimgwidth: ui.children[2].width,
      headimgheight: ui.children[2].height,
      membername: ui.children[3].content,
      membernamex: ui.children[3].x,
      membernamey: ui.children[3].y,
      membernamewidth: ui.children[3].width,
      membernameheight: ui.children[3].height,
      qrcode: ui.children[1].src,
      qrcodex: ui.children[1].x,
      qrcodey: ui.children[1].y,
      qrcodewidth: ui.children[1].width,
      qrcodeheight: ui.children[1].height
    };
  };

  //model2ui
  var parse2ui = function(meishi) {
    return {
      type: "set",
      children: [
        {
          type: "image",
          x: 0,
          y: 0,
          width: meishi.posterwidth,
          height: meishi.posterheight,
          src: meishi.posterurl
        },
        {
          type: "image",
          x: meishi.qrcodex,
          y: meishi.qrcodey,
          width: meishi.qrcodewidth,
          height: meishi.qrcodeheight,
          src: meishi.qrcode,
          hidden: true,
          candrag: true
        },
        {
          type: "image",
          x: meishi.headimgx,
          y: meishi.headimgy,
          width: meishi.headimgwidth,
          height: meishi.headimgheight,
          src: meishi.headimg,
          candrag: true
        },
        {
          type: "text",
          x: meishi.membernamex,
          y: meishi.membernamey,
          width: meishi.membernamewidth,
          height: meishi.membernameheight,
          content: meishi.membername,
          color: "blue",
          size: 30,
          candrag: true,
          inline: true
        }
      ]
    };
  };
  //数据的schema
  var schema = {
    type: "object",
    properties: {
      posterurl: { type: "string", title: "背景图" },
      posterwidth: { type: "integer", title: "背景图宽" },
      posterheight: { type: "integer", title: "背景图高" },

      headimg: { type: "string", title: "头像" },
      headimgx: { type: "integer", title: "头像横坐标" },
      headimgy: { type: "integer", title: "头像纵坐标" },
      headimgwidth: { type: "integer", title: "头像宽" },
      headimgheight: { type: "integer", title: "头像高" },

      membername: { type: "string", title: "会员名" },
      membernamex: { type: "integer", title: "会员名横坐标" },
      membernamey: { type: "integer", title: "会员名纵坐标" },
      membernamewidth: { type: "integer", title: "会员名宽" },
      membernameheight: { type: "integer", title: "会员名高" },

      qrcode: { type: "string", title: "二维码" },
      qrcodex: { type: "integer", title: "二维码横坐标" },
      qrcodey: { type: "integer", title: "二维码纵坐标" },
      qrcodewidth: { type: "integer", title: "二维码宽" },
      qrcodeheight: { type: "integer", title: "二维码高" }
    }
  };
  //对代码去除注释(正则表达式替换法)
  var removecomment = function(str) {
    return JSON.minify(str);
  };
  //对代码行增加注释
  var fillcomment = function(str) {
    var lines = str.split("\n");
    var result = "{\n";
    for (var index = 1; index < lines.length - 1; index++) {
      var code = lines[index];
      if (index == 1) {
        code += "//背景图";
      }
      if (index == 2) {
        code += "//背景图宽";
      }
      if (index == 3) {
        code += "//背景图高";
      }
      if (index == 4) {
        code += "//头像";
      }
      if (index == 5) {
        code += "//头像横坐标";
      }
      if (index == 6) {
        code += "//头像纵坐标";
      }
      if (index == 7) {
        code += "//头像宽";
      }
      if (index == 8) {
        code += "//头像高";
      }
      if (index == 9) {
        code += "//会员名";
      }
      if (index == 10) {
        code += "//会员名横坐标";
      }
      if (index == 11) {
        code += "//会员名纵坐标";
      }
      if (index == 12) {
        code += "//会员名宽";
      }
      if (index == 13) {
        code += "//会员名高";
      }

      if (index == 14) {
        code += "//二维码";
      }
      if (index == 15) {
        code += "//二维码横坐标";
      }
      if (index == 16) {
        code += "//二维码纵坐标";
      }
      if (index == 17) {
        code += "//二维码宽";
      }
      if (index == 18) {
        code += "//二维码高";
      }
      result += code + "\n";
    }
    result += "}";
    return result;
  };
  window.memeparser = {
    parse2model: parse2model,
    parse2ui: parse2ui,
    schema: schema,
    fillcomment: fillcomment,
    removecomment: removecomment
  };
})();
